package com.stomp;

import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author cj
 * @date 2018/11/19
 */
@Controller
public class StompController {

    /**
     *  MessageMapping注解表明被其修饰的方式用以处理消息的端点信息,是浏览器端send方法指定的地址
     *  所以服务端可以写无数个消息处理的方法,用@MessageMapping表明服务端接受信息的端点,用@SendTo表明消息发到哪里去.
     * @param message
     * @return
     * @throws Exception
     */
    @MessageMapping("/chatroom")
    @SendTo("/topic/messages")
    public OutputMessage handleMessage(Message message) throws Exception {
        String time = new SimpleDateFormat("HH:mm").format(new Date());
        OutputMessage outputMessage = new OutputMessage();
        outputMessage.setFrom(message.getFrom());
        outputMessage.setText(message.getText());
        outputMessage.setTime(time);
        return outputMessage;
    }
}
